package com.xiangying.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * MyUtils
 * 
 * @author FEMS Team
 * @version 1.0
 */

public class MyUtils {
    
    /** ID自增序列值 */
	public static int SERIES_NUMBER=0;
	
	private final static SimpleDateFormat _SDF=new SimpleDateFormat("yyMMddHHmmssSSS");
	
	/**
	 * 生成格式化的系统时间ID值（yyMMddHHmmssSSS）
	 * @return
	 */
	public synchronized static long GenerateTimeID() {
		if(SERIES_NUMBER==10000) {
			SERIES_NUMBER=0;
		}
		long new_id=Long.parseLong(_SDF.format(System.currentTimeMillis()))+(++SERIES_NUMBER);
		return new_id;
	}
    
    /**
     * 是否为数字
     * @param str
     * @return
     */
	public static boolean isNumeric(String str){ 
		Pattern pattern = Pattern.compile("[0-9]*"); 
		return pattern.matcher(str).matches(); 
	}
	
	/** 
     * 手机号验证 
     *  
     * @param  str 
     * @return 验证通过返回true 
     */  
    public static boolean isMobile(String str) {   
        Pattern p = Pattern.compile("^[1][3,4,5,8,7][0-9]{9}$"); // 验证手机号  
        Matcher m = p.matcher(str);  
        return m.matches();  
    } 
    
	/**
	 * 获取系统时间字符串
	 * @param dtFormat 时间格式(如yyyyMMddHHmmss)
	 * @return String
	 */
	public static String getDateTimeString(String dtFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dtFormat);
		return sdf.format(System.currentTimeMillis());
	}
	
	/**
	 * 获取指定时间的格式化字符串
	 * @param dt Date
	 * @param dtFormat 时间格式(如yyyyMMddHHmmss)
	 * @return String
	 */
	public static String getDateTimeString(Date dt, String dtFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dtFormat);
		return sdf.format(dt);
	}
	
	/**
	 * 获取年份
	 * @return
	 */
	public static String getDateYear(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		return sdf.format(System.currentTimeMillis());
	}
	
	/**
	 * 把时间字符串转换为Date类型
	 * @param dtStr 时间字符串
	 * @param dtFormat 时间格式(如yyyyMMddHHmmss)
	 * @return Date
	 */
	public static Date convertStr2Date(String dtStr, String dtFormat) {
		SimpleDateFormat df = new SimpleDateFormat(dtFormat);
		try {
			return df.parse(dtStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 生成UUID
	 * @return String
	 */
    public static String uniqueKey() {
        String key = UUID.randomUUID().toString();
        key = key.replace("-", "");
        return key.toLowerCase();
    }
    
    /**
	 * 获取指定字符数的随机数字字符串（首位不为0）
	 * @param num	字符个数
	 * @return String
	 */
	public static String getRandomNumStrByFirstNoZero(int num) {
		String rStr=new String();
		char[] keys=new char[]{'1','2','3','4','5','6','7','8','9','0'};
		Random rdm=new Random();
		for(int i=0;i<num;i++) {
			int index=rdm.nextInt(10);
			if(i==0&&index==9) {
				i--;
				continue;
			}
			rStr+=keys[index];
		}
		return rStr;
	}
	
	/**
	 * 获取指定字符数的随机数字字符串
	 * @param num	字符个数
	 * @return String
	 */
	public static String getRandomNumStr(int num) {
		String rStr=new String();
		char[] keys=new char[]{'1','2','3','4','5','6','7','8','9','0'};
		Random rdm=new Random();
		for(int i=0;i<num;i++) {
			int index=rdm.nextInt(10);
			rStr+=keys[index];
		}
		return rStr;
	}
	
	/**
	 * 获取指定字符数的随机字符串（包括数字和字母）
	 * @param num
	 * @return
	 */
	public static String getRandomStr(int num) {
		String rStr=new String();
		char[] numKeys=new char[]{'1','2','3','4','5','6','7','8','9','0'};
		char[] wordKeys=new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
				,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
		Random rdm=new Random();
		Random caseRdm=new Random();
		for(int i=0;i<num;i++) {
			int index;
			char c;
			int _case=caseRdm.nextInt(2);
			if(_case==0) {
				index=rdm.nextInt(10);
				c=numKeys[index];
			}else {
				index=rdm.nextInt(52);
				c=wordKeys[index];
			}
			rStr+=c;
		}
		return rStr;
	}
	
	/**
	 * 获取请求者的真实IP地址
	 * @param req
	 * @return String
	 */
	public static String getRequestIP(HttpServletRequest req) {
		if(req.getHeader("x-forwarded-for")==null) {
			return req.getRemoteAddr();
		}else {
			return req.getHeader("x-forwarded-for");
		}
	}
}